import {Page} from '@/base/Page';
import {Wishes} from '@/base/model/Wishes';
import {deleteWishesOperate, getWishesList, PutWishes} from '@/base/api/BannersApi';

class Form {
  wishName = '';
  companyid= 0;
  isAble = 0;
  companyname = '';
  agentname = '';
  phone = '';
}

export class WishesBloc {
  form = new Form();
  paged: Page<Wishes> = new Page<Wishes>([], 0, 1, 15, 0, []);
  loading = false;
  type: string | undefined;
  logintype: string | undefined;
  load(pageIndex: number, pageSize: number) {
    this.loading = true;
    return getWishesList({
      pageIndex,
      pageSize,
      sortDirection: 'DESC',
      wishName: this.form.wishName,
      companyName: this.form.companyname,
      agentName: this.form.agentname,
      phone: this.form.phone
    }).then((res) => {
      this.paged = res.data;
    }).finally(() => {
      this.loading = false;
    });
  }
  reset() {
    this.form = new Form();
    this.paged.pageIndex = 1;
    this.paged.pageSize = 15;
    this.load(this.paged.pageIndex, this.paged.pageSize);
  }
  removeItem(dateItem: Wishes) {
    this.loading = true;
    return deleteWishesOperate({id: dateItem.id})
      .finally(() => {
        this.loading = false;
      });
  }

  switchStatus(id, value, index) {
    if (value) {
      this.form.isAble = 1
    } else {
      this.form.isAble = 0
    }
    this.modifyItem(id, this.form.isAble)

  }

  modifyItem(
    wishId: number,
    isAble: number
  ) {
    this.loading = true;
    return PutWishes({
      wishId,
      isAble
    }).then((res) => {
      this.load(this.paged.pageIndex, this.paged.pageSize);
    }).finally(() => {
      this.loading = false;
    })
  }
}
